﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1.WebUI
{
	public class VoteContent : System.Web.UI.UserControl
	{
		private LoadedValue<int> loadedVoteId;
		public virtual int VoteId
		{
			get
			{
				if (loadedVoteId.Loader == null)
					loadedVoteId.Loader = () => int.Parse(Request.QueryString[GenericUrlManager.Vote_Query_VoteIdField]);
				return loadedVoteId.Value;
			}
		}

		private LoadedValue<Module.Vote> loadedVoteDetail;
		public virtual Module.Vote VoteDetail
		{
			get
			{
				if (loadedVoteDetail.Loader == null)
					loadedVoteDetail.Loader = () => Votes.GetItem(VoteId);
				return loadedVoteDetail.Value;
			}
		}

		private LoadedValue<bool> loadedOverridePageTitle;
		/// <summary>
		/// 是否重写页面标题。
		/// </summary>
		public virtual bool OverridePageTitle
		{
			get
			{
				if (loadedOverridePageTitle.Loader == null)
					loadedOverridePageTitle.Loader = () => BooleanExtensions.ConvertFromOrDefault(ViewState["OverridePageTitle"], true);
				return loadedOverridePageTitle.Value;
			}
			set
			{
				if (loadedOverridePageTitle.Setter == null)
					loadedOverridePageTitle.Setter = v => ViewState["OverridePageTitle"] = v;
				loadedOverridePageTitle.Value = value;
			}
		}

		public string ClassName
		{
			get
			{
				return VoteClasses.GetNameById(VoteDetail.ClassId);
			}
		}




		protected virtual void RewriteMeta()
		{
			if (OverridePageTitle)
				this.Page.Title = VoteDetail.Title +
					Hxsoft.AppV1.Page.TitleSectionSeparator + Res.Vote +
					Hxsoft.AppV1.Page.TitleSuffix;
		}
		
		protected override void OnPreRender(EventArgs e)
		{
			RewriteMeta();

			base.OnPreRender(e);
		}
	}
}